package com.chinamobile.maps.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.MDC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chinamobile.maps.dao.IUsersDAO;
import com.chinamobile.maps.orm.Users;
import com.chinamobile.maps.util.EncryptionMD5;
import com.chinamobile.maps.util.Struts2Util;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 2772939669445960595L;

	private String username;
	private String password;
	private String message;
	private IUsersDAO usersDao = null;

	private final Logger logger = LoggerFactory.getLogger(LoginAction.class);
	
	/**
	 * @return the usersDao
	 */
	public IUsersDAO getUsersDao() {
		return usersDao;
	}

	/**
	 * @param usersDao the usersDao to set
	 */
	public void setUsersDao(IUsersDAO usersDao) {
		this.usersDao = usersDao;
	}

	/**
	 * 
	 * 
	 * @return
	 */

	public String login() {
		if (this.username != null && this.password != null) {
			Users user = this.usersDao.getUserByName(this.username);
			if (user != null) {
				if (EncryptionMD5.md5.encodePassword(this.password,
						this.username).equals(user.getPassword())) {
					ActionContext actionContext = ActionContext.getContext();
					Map session = actionContext.getSession();
					session.put("username", this.username);
					session.put("role", user.getRole());
					session.remove("signinErrorMessage");
					MDC.put("loginName", this.username);
					HttpServletRequest request = Struts2Util.getRequest();
					MDC.put("remoteAddress",  request.getRemoteAddr());
					logger.info("登陆成功");
					return "success";
				}else{
					HttpServletRequest request = Struts2Util.getRequest();
					MDC.put("loginName", this.username);
					MDC.put("remoteAddress",  request.getRemoteAddr());
					logger.warn("登陆失败,密码错误!");
				}
			}else{
				HttpServletRequest request = Struts2Util.getRequest();
				MDC.put("loginName", this.username);
				MDC.put("remoteAddress",  request.getRemoteAddr());
				logger.warn("登陆失败，无效用户名!");
			}
		}
		ActionContext.getContext().getSession().put("signinErrorMessage", "用户名或密码错误!");
		return "loginerror";
	}

	public String logout(){
		ActionContext actionContext = ActionContext.getContext();
		actionContext.getSession().remove("username"); 
		actionContext.getSession().remove("role"); 
		actionContext.getSession().remove("signinErrorMessage"); 
		actionContext.getSession().isEmpty();
		logger.info("退出登陆");
		return "success";
	}
	public String test(){
		return "success";
	}
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
